package com.jiaoyou.baojian.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiaoyou.baojian.R;

/**
 * Created by DemonZYM on 17/5/6.
 */

public class OkDialog {
    private Dialog okDialog;

    public OkDialog(Context context, String msgTxt, final OnClickListener listener){
        this(context, msgTxt, "确认", listener);
    }

    public OkDialog(Context context, String msgTxt, String btnText, final OnClickListener listener){
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_ok, null);
        TextView msg = (TextView) view.findViewById(R.id.msg);
        Button btn = (Button) view.findViewById(R.id.btn);
        msg.setText(msgTxt);
        btn.setText(btnText);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
                listener.onBtnClick();
            }
        });
        // 创建自定义样式的Dialog
        okDialog = new Dialog(context, R.style.loading_dialog);
        okDialog.setCancelable(true);
        okDialog.setContentView(view, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
    }

    public void show(){
        okDialog.show();
    }

    public void close(){
        if (okDialog != null) {
            okDialog.dismiss();
        }
    }

    public interface OnClickListener{
        public void onBtnClick();
    }
}
